package xiao_jin.fantastic_journey.client;

import club.someoneice.cookie.event.EventBus;
import club.someoneice.cookie.event.SubEvent;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.client.render.fluid.v1.SimpleFluidRenderHandler;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.InputUtil;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import xiao_jin.fantastic_journey.client.renderer.RelictechTableRenderer;
import xiao_jin.fantastic_journey.core.handler.ItemTooltipEvent;
import xiao_jin.fantastic_journey.core.init.ModBlockEntity;
import xiao_jin.fantastic_journey.core.init.ModBlocks;
import xiao_jin.fantastic_journey.core.init.ModFluids;

import java.awt.*;

@Environment(EnvType.CLIENT)
public final class ClientEvent {
    private ClientEvent() {
    }

    public static void registerEventAndRender() {
        EventBus.register(ClientEvent.class);
        registerFluidEvents();
        renderBlocks();
    }

    private static void registerFluidEvents() {
        FluidRenderHandlerRegistry.INSTANCE.register(ModFluids.OIL, ModFluids.OIL_FLOWING, SimpleFluidRenderHandler.coloredWater(Color.BLACK.getRGB()));

        BlockRenderLayerMap.INSTANCE.putFluids(RenderLayer.getTranslucent(), ModFluids.OIL, ModFluids.OIL_FLOWING);
    }

    private static void renderBlocks() {
        /* Block Render */
        BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.RELICTECH_TABLE, RenderLayer.getCutout());
        BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.CULTIVATED_WASTE_SOIL, RenderLayer.getCutout());

        /* TileEntity Render */
        BlockEntityRendererFactories.register(ModBlockEntity.RELICTECH_TABLE_BLOCK_ENTITY, RelictechTableRenderer::new);
    }

    @SubEvent
    public static void appendTooltipForItem(ItemTooltipEvent event) {
        if (event.item.isEmpty()) return;

        final var itemStack = event.item;

        var infoDescription = "description." + Registries.ITEM.getId(itemStack.getItem());
        infoDescription = I18n.hasTranslation(infoDescription) ? I18n.translate(infoDescription) : "";

        var infoTroll = String.format("troll.%s", Registries.ITEM.getId(itemStack.getItem()));
        infoTroll = I18n.hasTranslation(infoTroll) ? I18n.translate(infoTroll) : "";

        if (!infoDescription.isEmpty()) {
            final var flag = InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), GLFW.GLFW_KEY_LEFT_SHIFT);
            var textList = (!flag
                    ? I18n.translate("description.lint", getKeyName(InputUtil.GLFW_KEY_LEFT_SHIFT))
                    : infoDescription).split("/n");

            for (String s : textList) event.tooltip.add(Text.of(s));
        }

        if (!infoTroll.isEmpty()) {
            final var flag = InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), GLFW.GLFW_KEY_LEFT_CONTROL);
            var textList = (!flag
                    ? I18n.translate("troll.lint", getKeyName(InputUtil.GLFW_KEY_LEFT_CONTROL))
                    : infoTroll).split("/n");

            for (String s : textList) event.tooltip.add(Text.of(s));
        }
    }

    private static String getKeyName(int keyCode) {
        return InputUtil.Type.KEYSYM.createFromCode(keyCode).getLocalizedText().getString();
    }
}
